<?php
class PayPalGateway extends PaymentGatewayAbstract {

    protected $_gateway;

    // Support
    protected $_supportedCurrencies = array(
            // 'ARS', // Supported by 2Checkout, but not by PayPal
            'AUD',
            'BRL', // This currency is supported as a payment currency and a currency balance for in-country PayPal accounts only.
            'CAD',
            'CHF',
            'CZK', // Not supported by 2Checkout
            'DKK',
            'EUR',
            'GBP',
            'HKD',
            'HUF', // Not supported by 2Checkout
            'ILS', // Not supported by 2Checkout
            //'INR', // Supported by 2Checkout
            'JPY',
            'MXN',
            'MYR', // Not supported by 2Checkout - This currency is supported as a payment currency and a currency balance for in-country PayPal accounts only.
            'NOK',
            'NZD',
            'PHP', // Not supported by 2Checkout
            'PLN', // Not supported by 2Checkout
            'SEK',
            'SGD', // Not supported by 2Checkout
            'THB', // Not supported by 2Checkout
            'TWD', // Not supported by 2Checkout
            'USD',
            //'ZAR', // Supported by 2Checkout
    );

    protected $_supportedLanguages = array(
            'es', 'en', 'de', 'fr', 'nl', 'pt', 'zh', 'it', 'ja', 'pl',
            // Full
            //'es_AR', 'en_AU', 'de_AT', 'en_BE', 'fr_BE', 'nl_BE', 'pt_BR', 'en_CA',
            //'fr_CA', 'zh_CN', 'zh_HK', 'fr_FR', 'de_DE', 'it_IT', 'ja_JP', 'es_MX',
            //'nl_NL', 'pl_PL', 'en_SG', 'es_SP', 'fr_CH', 'de_CH', 'en_CH', 'en_GB',
            //'en_US',
            // Not supported
            //'de_BE', 'zh_SG', 'gsw_CH', 'it_CH',
    );

    protected $_supportedRegions = array(
            'AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM',
            'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ',
            'BM', 'BT', 'BO', 'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BG', 'BF', 'BI',
            'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO',
            'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 'HR', 'CU', 'CY', 'CZ', 'DK', 'DJ',
            'DM', 'DO', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ',
            'FI', 'FR', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR',
            'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY', 'HT', 'HM', 'VA',
            'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT',
            'JM', 'JP', 'JE', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA',
            'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW',
            'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD',
            'MC', 'MN', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'AN', 'NC',
            'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS',
            'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO',
            'RU', 'RW', 'SH', 'KN', 'LC', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN',
            'CS', 'SC', 'SL', 'SG', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'ES', 'LK',
            'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL',
            'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE',
            'GB', 'US', 'UM', 'UY', 'UZ', 'VU', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH',
            'YE', 'ZM',
    );

    protected $_supportedBillingCycles = array(
            /* 'Day', */ 'Week', /* 'SemiMonth',*/ 'Month', 'Year',
    );


    // Translation

    protected $_transactionMap = array(
            Transaction::REGION => 'LOCALECODE',
            Transaction::RETURN_URL => 'RETURNURL',
            Transaction::CANCEL_URL => 'CANCELURL',

            // Deprecated?
            Transaction::IPN_URL => 'NOTIFYURL',
            Transaction::VENDOR_ORDER_ID => 'INVNUM',
            Transaction::CURRENCY => 'CURRENCYCODE',
            Transaction::REGION => 'LOCALECODE',
    );



    // General

    /**
     * Constructor
     *
     * @param array $options
     */
    public function  __construct(array $options = null) {
        parent::__construct($options);

        if( null === $this->getGatewayMethod() ) {
            $this->setGatewayMethod('POST');
        }
    }

    /**
     * Get the service API
     *
     * @return Engine_Service_PayPal
     */
    public function getService() {
        /*
		 if( null === $this->_service ) {
			$this->_service = new PayPalService(array_merge(
			$this->getConfig(),
			array(
			'testMode' => $this->getTestMode(),
			)
			));
			}
        */

        if( null === $this->_service ) {
            $this->_service = new PayPalService($this->getConfig());
        }
        return $this->_service;
    }

    public function getGatewayUrl() {
        // Manual
        if( null !== $this->_gatewayUrl ) {
            return $this->_gatewayUrl;
        }

        // Auto
        if( $this->getTestMode() ) {
            return 'https://www.sandbox.paypal.com/cgi-bin/webscr';
        } else {
            return 'https://www.paypal.com/cgi-bin/webscr';
        }
    }



    // IPN

    public function processIpn(Engine_Payment_Ipn $ipn) {
        // Validate ----------------------------------------------------------------

        // Get raw data
        $rawData = $ipn->getRawData();

        // Log raw data
        //if( 'development' === APPLICATION_ENV ) {
        $this->_log(print_r($rawData, true), Zend_Log::DEBUG);
        //}

        // Check a couple things in advance
        if( !empty($rawData['test_ipn']) && !$this->getTestMode() ) {
            throw new Engine_Payment_Gateway_Exception('Test IPN sent in non-test mode');
        }

        // @todo check the email address of the account?

        // Build url and post data
        $parsedUrl = parse_url($this->getGatewayUrl());
        $postString = http_build_query(array_merge(array(
                'cmd' => '_notify-validate',
                ), $rawData));

        if( empty($parsedUrl['host']) ) {
            $this->_throw(sprintf('Invalid host in gateway url: %s', $this->getGatewayUrl()));
            return false;
        }
        if( empty($parsedUrl['path']) ) {
            $parsedUrl['path'] = '/';
        }

        // Open socket
        $fp = fsockopen($parsedUrl['host'], 80, $errNum, $errStr, 30);
        if( !$fp ) {
            $this->_throw(sprintf('Unable to open socket: [%d] %s', $errNum, $errStr));
        }
        stream_set_blocking($fp, true);

        fputs($fp, "POST {$parsedUrl['path']} HTTP/1.1\r\n");
        fputs($fp, "Host: {$parsedUrl['host']}\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: " . strlen($postString) . "\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $postString . "\r\n\r\n");

        $response = '';
        while( !feof($fp) ) {
            $response .= fgets($fp, 1024);
        }

        fclose($fp);

        if( !stripos($response, 'VERIFIED') ) {
            $this->_throw(sprintf('IPN Validation Failed: %s %s', $parsedUrl['host'], $parsedUrl['path']));
            return false;
        }

        // Success!
        $this->_log('IPN Validation Succeeded');



        // Process -----------------------------------------------------------------
        $rawData = $ipn->getRawData();

        $data = $rawData;

        return $data;
    }



    // Transaction
    public function processTransaction(Transaction $transaction) {
        $data = array();
        $rawData = $transaction->getRawData();

        // Driver-specific params
        if( isset($rawData['driverSpecificParams']) ) {
            if( isset($rawData['driverSpecificParams'][$this->getDriver()]) ) {
                $data = array_merge($data, $rawData['driverSpecificParams'][$this->getDriver()]);
            }
            unset($rawData['driverSpecificParams']);
        }

        // Add default region?
        if( empty($rawData['region']) && ($region = $this->getRegion()) ) {
            $rawData['region'] = $region;
        }

        // Add default currency
        if( empty($rawData['currency']) && ($currency = $this->getCurrency()) ) {
            $rawData['currency'] = $currency;
        }


        // Process abtract translation map
        $tmp = array();
        $data = array_merge($data, $this->_translateTransactionData($rawData, $tmp));
        $rawData = $tmp;


        // Call setExpressCheckout
        //echo "<pre>" . print_r($data, true). "</pre>";
        //die();

        $token = $this->getService()->setExpressCheckout($data);

        $data = array();
        $data['cmd'] = '_express-checkout';
        $data['token'] = $token;

        return $data;
    }



    // Admin
    public function test() {
        try {
            $this->getService()->searchButtons(time());
        } catch( PayPalException $e ) {
            if( in_array((int) $e->getCode(), array(10002, 10008, 10101) ) ) {
                throw new ExceptionAbstract(sprintf('Gateway login ' .
                'failed. Please double-check ' .
                'your connection information. ' .
                'The message was: %1$s', $e->getMessage()));
            }
        }

        return true;
    }

    /**
     * Create a transaction object from specified parameters
     *
     * @return Transaction
     */
    public function createTransaction(array $params) {
        $transaction = new Transaction($params);
        $transaction->process($this);
        return $transaction;
    }

    /**
     * Create a transaction for a subscription
     * @param DealOrderEntity $deal
     * @param array $params
     * @return Transaction
     */
    public function createSubscriptionTransaction(DealOrderEntity $deal, array $params = array(), $isRecurring = true) {

        $params['driverSpecificParams']['PayPal'] = array(
                'BILLINGTYPE' => 'RecurringPayments',
                'CURRENCYCODE' => 'GBP',
                'BILLINGAGREEMENTDESCRIPTION' => 'sale');//$deal->getDealDescription());



        // Create transaction
        $transaction = $this->createTransaction($params);
        return $transaction;
    }



    /**
     * Process return of subscription transaction
     *
     * @param DealOrderEntity $order
     * @param array $params
     */
    public function onSubscriptionTransactionReturn(
            DealOrderEntity $order, array $params = array()) {
        // get DealOption
        $dealDataService = new DealsDataService();
        $deal = $dealDataService->getById($order->deal_id);
        /*
		 // Check subscription state
		 if( $subscription->status == 'active' ||
		 $subscription->status == 'trial') {
			return 'active';
			} else if( $subscription->status == 'pending' ) {
			return 'pending';
			}
        */

        // Check for cancel state - the user cancelled the transaction
        if( $params['state'] == 'cancel' ) {
            // Cancel order and subscription?
            //$order->onCancel();
            //$subscription->onPaymentFailure();
            // Error
            throw new ExceptionAbstract('Your payment has been cancelled and ' .
                    'not been charged. If this is not correct, please try again later.');
        }

        // Check params
        if( empty($params['token']) ) {
            // Cancel order and subscription?
            //$order->onFailure();
            //$subscription->onPaymentFailure();
            // This is a sanity error and cannot produce information a user could use
            // to correct the problem.
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }

        // Get details
        try {
            $data = $this->getService()->detailExpressCheckout($params['token']);
        } catch( Exception $e ) {
            // Cancel order and subscription?
            //$order->onFailure();
            //$subscription->onPaymentFailure();
            // This is a sanity error and cannot produce information a user could use
            // to correct the problem.
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }

        // Let's log it
        $this->getGateway()->getLog()->log('ExpressCheckoutDetail: '
                . print_r($data, true), Zend_Log::INFO);

        // Check for errors
        if( empty($data) ) {
            // This is a sanity error and cannot produce information a user could use
            // to correct the problem.
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }
        else if( empty($data['BILLINGAGREEMENTACCEPTEDSTATUS']) ||
                '0' == $data['BILLINGAGREEMENTACCEPTEDSTATUS'] ) {
            // Cancel order and subscription?
            //$order->onCancel();
            //$subscription->onPaymentFailure();
            // Error
            throw new ExceptionAbstract('Your payment has been cancelled and ' .
                    'not been charged. If this in not correct, please try again later.');
        } else if( !isset($data['PAYMENTREQUESTINFO'][0]['ERRORCODE']) ||
                '0' != $data['PAYMENTREQUESTINFO'][0]['ERRORCODE'] ) {
            // Cancel order and subscription?
            //$order->onFailure();
            //$subscription->onPaymentFailure();
            // This is a sanity error and cannot produce information a user could use
            // to correct the problem.
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }

        // Create recurring payments profile
        $rpData = array(
                'TOKEN' => $params['token'],
                'PROFILEREFERENCE' => $order->id,
                'CURRENCYCODE' => 'GBP',
                'PROFILESTARTDATE' => $data['TIMESTAMP'],
                'DESC' => '123123',
                'BILLINGPERIOD' => 'Month',
                'BILLINGFREQUENCY' => 1,
                'INITAMT' => 0,
                'AMT' => $order->total_price,
                'TOTALBILLINGCYCLES' => 0,	//Total Billing Cycle
        );

        $this->getGateway()->getLog()->log('CreateRecurringPaymentsProfile: '
                . print_r($rpData, true), Zend_Log::INFO);

        // Create recurring payment profile
        try {
            $rdata = $this->getService()->createRecurringPaymentsProfile($rpData);
//                print_r($rdata,true);die;
        } catch( Exception $e ) {
            // Cancel order and subscription?
            //$order->onFailure();
            //$subscription->onPaymentFailure();
            // This is a sanity error and cannot produce information a user could use
            // to correct the problem.
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }

        // Let's log it
        $this->getGateway()->getLog()->log('CreateRecurringPaymentsProfile: '
                . print_r($rdata, true), Zend_Log::INFO);

        // Check returned profile id
        if( empty($rdata['PROFILEID']) ) {
            // Cancel order and subscription?
            //$order->onFailure();
            //$subscription->onPaymentFailure();
            // This is a sanity error and cannot produce information a user could use
            // to correct the problem.
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }
        $profileId = $rdata['PROFILEID'];

        // Update order with profile info and complete status?
        // Save ProfileId
        $order->status = DealOrderEntity::COMPLETED;
//          $order->profileId = $profileId;

        $dealOrderDataService = new DealOrderDataService();
        $dealOrderDataService->update($order);

        // Check profile status
        if( $rdata['PROFILESTATUS'] == 'ActiveProfile' ) {
            //return 'active';
            return SUCC;
        } else if( $rdata['PROFILESTATUS'] == 'PendingProfile' ) {
            return ERROR;
        } else {
            // Cancel order and subscription?
            //$order->onFailure();
            //$subscription->onPaymentFailure();
            // This is a sanity error and cannot produce information a user could use
            // to correct the problem.
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }
    }

    public function onTransactionReturn(DealOrderEntity  $order, array $params = array(), $currencyCode = "USD") {
        $DealOrderDataService = new DealOrderDataService();
        // Check for cancel state - the user cancelled the transaction
        if( $params['state'] == 'cancel' ) {
            // Cancel order and subscription?
            //$order->onCancel();
            //$subscription->onPaymentFailure();
            // Error
            throw new ExceptionAbstract('Your payment has been cancelled and ' .
                    'not been charged. If this is not correct, please try again later.');
        }

        // Check params
        if( empty($params['token']) ) {
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }

//        $this->getGateway()->getLog()->log("Test Log", Zend_Log::INFO);

        // Get details
        try {
            $data = $this->getService()->detailExpressCheckout($params['token']);
        } catch( Exception $e ) {
            $this->getGateway()->getLog()->log('DetailExpressCheckoutError: '
                    . $e->__toString(), Zend_Log::ERR);

            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }

        // Let's log it
        $this->getGateway()->getLog()->log('ExpressCheckoutDetail: '
                . print_r($data, true), Zend_Log::INFO);

        // Do payment
        try {
            $rdata = $this->getService()->doExpressCheckoutPayment($params['token'],
                    $params['PayerID'], array(
                    'PAYMENTACTION' => 'Sale',
                    'AMT' => $data['AMT'],
                    'CURRENCYCODE' => $currencyCode,
            ));
        } catch( Exception $e ) {
            // Log the error
            $this->getGateway()->getLog()->log('DoExpressCheckoutPaymentError: '
                    . $e->__toString(), Zend_Log::ERR);

            // Cancel order and subscription?
            //$order->onFailure();
            //$subscription->onPaymentFailure();
            // This is a sanity error and cannot produce information a user could use
            // to correct the problem.
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }

        // Let's log it
        $this->getGateway()->getLog()->log('DoExpressCheckoutPayment: '
                . print_r($rdata, true), Zend_Log::INFO);

        // Get payment state
        $paymentStatus = null;
        $orderStatus = null;
        switch( strtolower($rdata['PAYMENTINFO'][0]['PAYMENTSTATUS']) ) {
            case 'created':
            case 'pending':
                $paymentStatus = 'okay';
                $orderStatus = 'complete';
                break;

            case 'completed':
            case 'processed':
            case 'canceled_reversal': // Probably doesn't apply
                $paymentStatus = 'okay';
                $orderStatus = 'complete';
                break;

            case 'denied':
            case 'failed':
            case 'voided': // Probably doesn't apply
            case 'reversed': // Probably doesn't apply
            case 'refunded': // Probably doesn't apply
            case 'expired':  // Probably doesn't apply
            default: // No idea what's going on here
                $paymentStatus = 'failed';
                $orderStatus = 'failed'; // This should probably be 'failed'
                break;
        }

        // Update status for payment
        $order->gateway_transaction_id = $rdata['PAYMENTINFO'][0]['TRANSACTIONID'];
        $DealOrderDataService->update($order);
        // Check payment status
        if( $paymentStatus == 'okay'  ) {
            return SUCC;
        }
        else if( $paymentStatus == 'failed' ) {
            // Cancel order and subscription?
            //$order->onFailure();
            //$subscription->onPaymentFailure();
            // Payment failed
            throw new ExceptionAbstract('Your payment could not be ' .
                    'completed. Please ensure there are sufficient available funds ' .
                    'in your account.');
        }
        else {
            // This is a sanity error and cannot produce information a user could use
            // to correct the problem.
            throw new ExceptionAbstract('There was an error processing your ' .
                    'transaction. Please try again later.');
        }
    }

    public function getGateway() {
        if( null === $this->_gateway ) {
            $gateway = new PayPalGateway();
            if( !($gateway instanceof PaymentGatewayAbstract) ) {
                throw new ExceptionAbstract('Plugin class not instance of Engine_Payment_Gateway');
            }
            $this->_gateway = $gateway;
        }

        return $this->_gateway;
    }

    public function getDriver() {
        if($this->_driver === NULL) {
            $this->_driver = "PayPal";
        }

        return $this->_driver;
    }

    public function cancelSubscription($profileId, $note = null) {
        try {
            $result = $this->getService()->cancelRecurringPaymentsProfile($profileId, $note);
            //Log to Payment Log file
            $this->getGateway()->getLog()->log('CancelRecurringPaymentsProfile: '
                    . print_r($result, true), Zend_Log::INFO);

        } catch( Exception $e ) {
            // throw?
        }

        return $this;
    }
    public function createBuyNowTransaction($groupon, $deal,  array $params = array()) {
		$settingDataService = new SettingsDataService();
		$curencySettingsEntity = $settingDataService->getItem ( CURRENCY_USD_VND );
        $params['driverSpecificParams']['PayPal'] = array(
                'AMT' => round($params['price'] / $curencySettingsEntity->value, 2),
                'DESC' => 'sale',
                'CUSTOM' => $deal->id,
                'INVNUM' => $params['vendor_order_id'],
                'ITEMAMT' => round($params['price'] / $curencySettingsEntity->value, 2),
                'ITEMS' => array(
                        array(
                                'NAME' => $groupon->title,
                                'DESC' => 'sale',
                                'AMT' => round($deal->sale_price / $curencySettingsEntity->value, 2),
                                'NUMBER' => $deal->id,
                                'QTY' => $params['amount'] ,
                        ),
                )
        );

        // Create transaction
        $transaction = $this->createTransaction($params);
        return $transaction;
    }
    public function refundForBuyer($deal, $groupon, $buyer) {

        //Prepare data for payment
        $params = array();
        $params['EMAILSUBJECT'] = "Refund for buyer ID: $buyer->id of groupon title [{$groupon->title}] completed";
        $params['RECEIVERTYPE'] = "EmailAddress";
        $params['CURRENCYCODE'] = 'USD';
        //$params['L_EMAIL0'] = $account->paypal_email;
        //total money must pay to owner

        $params['L_Amt0'] = $buyer->total_price;
        $params['L_NOTE0'] = "Refund for buyer ID:$buyer->id of groupon title [{$groupon->title}] completed";
        try {
//            $paypalConfig = $this->settingsDataService->getPaypalSettings();
//            $paypalGateway = new PayPalGateway($paypalConfig);
//            $paypalGateway->setConfig($paypalConfig);
            $response = $this->getService()->refundTransaction($buyer->gateway_transaction_id, null,'Full',$buyer->total_price,$params['CURRENCYCODE'], 'refund');

            $this->getGateway()->getLog()->log('Refund Successful: '
                    . print_r($response, true), Zend_Log::INFO);
            return true;
        }
        catch (Engine_Service_PayPal_Exception $e) {
            // Let's log it
            $this->getGateway()->getLog()->log('Refund Got Exception: '
                    . print_r($e->getMessage(), true), Zend_Log::ERR);
            return false;
        }
    }
}
